<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-radio-group style="position: relative;top: -4px;" v-model="form.formal" @change="onFormal">
                    <el-radio-button label="编内" :value="1" />
                    <el-radio-button label="编外" :value="0" />
                </el-radio-group>
                <el-input v-model="form.search" class="ml10" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <el-table class="mt20" :data="tableData" border v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
            <el-table-column label="姓名"  align="center">
                <template #default="scope">
                    <el-link @click="onEdit(scope.row.id)" >{{scope.row.name}}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="posts" label="岗位" align="center" />
            <el-table-column prop="mobile" label="手机号码" align="center" />
            <el-table-column prop="formal_text" label="编内\编外" align="center" />
            <el-table-column label="邮箱">
                <template #default="scope">
                    <el-text v-if="scope.row.more" >{{scope.row.more.email}}</el-text>
                </template>
            </el-table-column>
            <el-table-column prop="employees_text" label="在职/离职" align="center"  />
            <el-table-column fixed="right" label="操作" width="150" align="center">
                <template #default="scope">
                    <el-link type="primary" @click="onShow(scope.row)">查看</el-link>
                    <el-link type="primary" @click="onEdit(scope.row.id)">编辑</el-link>
                    <el-popconfirm name="确定要删除么?" @confirm="onDelete(scope.row.id)">
                        <template #reference>
                            <el-link type="danger" >删除</el-link>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>

    <el-dialog v-model="isProject" title="参与项目列表" width="60%" draggable>
        <component :is="Project" :data = userId @saveBind="saveBind" />
    </el-dialog>

    <el-dialog v-model="isShow" title="基本信息" width="1200px" draggable>
        <el-row>
            <el-col :span="18">
                <el-descriptions border :column="2" >
                    <el-descriptions-item label="姓名　　　　" >{{info.name}}</el-descriptions-item>
                    <el-descriptions-item label="出生日期　　" >{{info.birth_date}}</el-descriptions-item>
                    <el-descriptions-item label="证件类型　　" >{{info.id_type == 1 ? '身份证' : '护照'}}</el-descriptions-item>
                    <el-descriptions-item label="证件号码　　" >{{info.idcard}}</el-descriptions-item>
                    <el-descriptions-item label="民族　　　　" >{{info.ethnicity}}</el-descriptions-item>
                    <el-descriptions-item label="性别　　　　" >{{info.sex == 1 ? '男' : '女'}}</el-descriptions-item>
                    <el-descriptions-item label="政治面貌　　" >{{info.political_status}}</el-descriptions-item>
                    <el-descriptions-item label="文化程度　　" >{{info.education}}</el-descriptions-item>
                    <el-descriptions-item :span="2" label="户籍所在地址">{{info.household_register}}</el-descriptions-item>
                    <el-descriptions-item :span="2" label="居住地址">{{info.residential_address}}</el-descriptions-item>
                    <el-descriptions-item :span="2" :label="info.contract_type">
                        {{ info.contract_start_date }}起至{{ info.contract_start_date }}日止，其试用期自 {{ info.trial_start_period }}起至{{ info.trial_end_period }}日止
                    </el-descriptions-item>
                </el-descriptions>
            </el-col>
            <el-col :span="6">
                <div class="text-center"><el-image style="width: 120px; height: 100%" :src="info.avatar" /></div>
            </el-col>
        </el-row>
    </el-dialog>

</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import { CirclePlus, RefreshRight, Search } from '@element-plus/icons-vue'
import Project from './components/project.vue';
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import router from '../../../router';
const indexMethod = (index: number) => {
  return index+= 1
}
const form = ref<any>({search:'',role_type:1,formal:1,page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
const isProject = ref<boolean>(false)
interface User {
    id: number
    name: string
    mobile: string
    projects: number
    created_at: string
}
const isShow = ref<boolean>(false)
const info = ref<any>({})
const userId = ref<number>(0)
const tableData = ref<User[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/user',form.value).then((response:any)=>{
        form.value.loading = false
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 0
    onGetData()
}

const onFormal = (data: number) => {
    form.value.formal = data
    form.value.page = 0
    onGetData()
}

const onShow = (data: any) => {
    info.value = data
    isShow.value = true
}

const onEdit = (id: number) => {
    router.push({ name: 'hrUserEdit',params: { id: id }})
}

// const onStatus = (id: number) => {
//     http.patch('/user/status',{id:id}).then((response:any)=>{
//         if(response.data.code != 200){
//             ElMessage.error(response.data.msg);return;
//         }
//         ElMessage.success(response.data.msg);
//         onGetData();
//     })
// }

// const onProject = (id: number) => {
//     userId.value = id
//     isProject.value = true
// }

const onDelete = (id: number) => {
    http.delete('/user/'+id).then((response:any)=>{
        if(response.data.code != 200){
            ElMessage.error(response.data.msg);return;
        }
        ElMessage.success(response.data.msg);
        onGetData();
    })
}

const saveBind = () => {
    isProject.value = false
    onGetData()
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>